package com.ruoyi.bzjgl.service.impl;

import com.ruoyi.bzjgl.domain.LegalCaseBasic;
import com.ruoyi.bzjgl.domain.TreeAjxz;
import com.ruoyi.bzjgl.mapper.CaseNatureMapper;
import com.ruoyi.bzjgl.service.ICaseNatureService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CaseNatureServiceImpl implements ICaseNatureService {
    @Autowired
    private CaseNatureMapper caseNatureMapper;

    @Override
    public List<TreeAjxz> selectAjxz(String mc) {
        List<LegalCaseBasic> ajxzs =  caseNatureMapper.selectAjxz(mc);
        return buildAjxzTreeSelect(ajxzs);
    }

    @Override
    public int insertCaseNature(LegalCaseBasic legalCaseBasic) {
        String uuid16 = UUID.randomUUID().toString().replace("-", "").substring(0,16);
        legalCaseBasic.setBh(uuid16);
        return caseNatureMapper.insertCaseNature(legalCaseBasic);
    }

    @Override
    public List<LegalCaseBasic> findChild(String xh) {
        return caseNatureMapper.findChildList(xh);
    }

    @Override
    public int updateCaseNature(LegalCaseBasic legalCaseBasic) {
        return caseNatureMapper.updateCaseNature(legalCaseBasic);
    }

    @Override
    public int deleteCaseNature(String xh) {
        return caseNatureMapper.deleteCaseNature(xh);
    }



    private List<TreeAjxz> buildAjxzTreeSelect(List<LegalCaseBasic> ajxzs){
        List<LegalCaseBasic> returnList = new ArrayList<LegalCaseBasic>();
        List<String> tempList = ajxzs.stream().map(LegalCaseBasic::getBh).collect(Collectors.toList());
        for (LegalCaseBasic legal : ajxzs)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if ("ROOT".equals(legal.getSjbh()))
            {
                recursionFn(ajxzs, legal);
                returnList.add(legal);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = ajxzs;
        }
        return returnList.stream().map(TreeAjxz::new).collect(Collectors.toList());
    }


    private void recursionFn(List<LegalCaseBasic> list, LegalCaseBasic t)
    {
        // 得到子节点列表
        List<LegalCaseBasic> childList = getChildList(list, t);
        t.setChildren(childList);
        for (LegalCaseBasic tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }


    private List<LegalCaseBasic> getChildList(List<LegalCaseBasic> list, LegalCaseBasic t)
    {
        List<LegalCaseBasic> tlist = new ArrayList<LegalCaseBasic>();
        Iterator<LegalCaseBasic> it = list.iterator();
        while (it.hasNext())
        {
            LegalCaseBasic n = (LegalCaseBasic) it.next();
            if (StringUtils.isNotNull(n.getSjbh())){
                if(n.getSjbh().equals(t.getBh())){
                    tlist.add(n);
                }
            }
        }
        return tlist;
    }


    private boolean hasChild(List<LegalCaseBasic> list, LegalCaseBasic t)
    {
        return getChildList(list, t).size() > 0;
    }

}
